package com.si.anddos;

import com.si.anddos.dpadapters.AnddosDbAdapter;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;


/**
 * A class representing New Account activity in anddos application
 * @author Carlos Martinez
 *
 */
public class AccountNewActivity extends OptionsMenuActivity {
	private AnddosDbAdapter mDbHelper;
    private EditText mUsernameText;
    private EditText mPasswordText;
    private Spinner mServiceSpinner;
    private Button saveButton;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDbHelper = new AnddosDbAdapter(this);
        
        
        setContentView(R.layout.account_new);
        setTitle(R.string.titleNewAccount);

        mUsernameText = (EditText) findViewById(R.id.editTextUsername);
        mPasswordText = (EditText) findViewById(R.id.editTextPassword);
        mServiceSpinner = (Spinner) findViewById(R.id.spinnerService);

        // fill spinner with data from resources
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.services_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mServiceSpinner.setAdapter(adapter);
                
        saveButton = (Button) findViewById(R.id.buttonSave);    
        
        // save button click listener
        saveButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
            	// check for empty fields
            	if(TextUtils.isEmpty(mUsernameText.getText()) || TextUtils.isEmpty(mPasswordText.getText()))
            	{
            		createAlertEmptyUserOrPass();
            	}
            	else
            	{
            		saveAccount();
            		setResult(RESULT_OK);
            		finish();
            	}
            }
        });
    }
    
    // nahrada za tlacitko cancel
    @Override
	public void onBackPressed() {
		setResult(RESULT_OK);
		finish();
	}
    
    private void saveAccount() {
    	String service = mServiceSpinner.getSelectedItem().toString();
        String username = mUsernameText.getText().toString();
        String password = mPasswordText.getText().toString();

        mDbHelper.open();
        // save account to database
        // add check if acounts info exists on rapidshare!!
        long id = mDbHelper.createAccount(service, username, password);
        if (id > 0) {
        	// add fail to store into database
        }
        
        mDbHelper.close();

    }
    
    private void createAlertEmptyUserOrPass()
    {
    	// create alert dialog if user left empty fields (username or password)
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle(R.string.alertSaveNewAccountTitle)
    			.setMessage(R.string.alertSaveNewAccountMessage)
    			.setCancelable(false)
    			.setNegativeButton(R.string.alertSaveNegButton, new DialogInterface.OnClickListener() {
    				public void onClick(DialogInterface dialog, int id) {
    					dialog.cancel();
    				}
    			});
    	AlertDialog alert = builder.create();
    	alert.show();
    }

}


